package com.baymax.test.scene.demo

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View


class CircleProgressView : View {

    private val mRectF: RectF = RectF()
    private val innerRectF: RectF = RectF()

    private var mViewCenterX = 0 //view宽的中心点(可以暂时理解为圆心)
    private var mViewCenterY = 0 //view高的中心点(可以暂时理解为圆心)

    private var mCirclePath = 20f //最里面白色圆的半径
    private val mRingWidth = 4f //圆环的宽度
    private val mMinCircleColor = Color.WHITE //最里面圆的颜色
    private val mRingNormalColor = Color.WHITE//默认圆环的颜色
    private val mProgressColor = Color.WHITE //默认圆环的颜色

//    private val mInnerCirclePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
//        color = mMinCircleColor
//    }

    private val mOuterCirclePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = mRingWidth
        color = mRingNormalColor //圆环默认颜色为灰色
    }
    private val innerCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        color = mProgressColor
    }
    private var progress = 0f

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    )

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        //view的宽和高,相对于父布局(用于确定圆心)
        //view的宽和高,相对于父布局(用于确定圆心)
        mViewCenterX = width / 2
        mViewCenterY = height / 2
//        mCirclePath = Math.min(viewHeight, viewWidth) / 2f
        //画矩形
        //画矩形
        mRectF.set(
            mViewCenterX - mRingWidth - mCirclePath / 2,
            mViewCenterY - mRingWidth - mCirclePath / 2,
            mViewCenterX + mRingWidth + mCirclePath / 2,
            mViewCenterY + mRingWidth + mCirclePath / 2,
        )
        innerRectF.set(mRectF)
        innerRectF.inset(mRingWidth / 2 - 1, mRingWidth / 2 - 1)

    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

//        canvas.drawCircle(
//            mViewCenterX.toFloat(), mViewCenterY.toFloat(),
//            Math.min(width, height) / 2f, mInnerCirclePaint
//        )
        //画默认圆环
        drawOuterRing(canvas)
        //画彩色圆环
        drawInnerCircle(canvas)
    }

    /**
     * 画默认圆环
     *
     * @param canvas
     */
    private fun drawOuterRing(canvas: Canvas) {
        canvas.drawArc(mRectF, 360f, 360f, true, mOuterCirclePaint)
    }

    /**
     * 画彩色圆环
     *
     * @param canvas
     */
    private fun drawInnerCircle(canvas: Canvas) {

        //逆时针旋转90度
        canvas.rotate(-90f, mViewCenterX.toFloat(), mViewCenterY.toFloat())
        canvas.drawArc(innerRectF, 360f, progress, true, innerCirclePaint)
    }

    //***************************************用于更新圆环表示的数值*****************************************************
    //***************************************用于更新圆环表示的数值*****************************************************
    /**
     * 设置当前值
     *
     * @param value
     */
    fun setValue(value: Int) {
        progress = (360 * (value / 100f))
        postInvalidate()
//        startAnimator(start, end, 3000)
    }

    private fun startAnimator(start: Int, end: Int, animTime: Long) {
        val valueAnimator = ValueAnimator.ofInt(start, end)
        valueAnimator.duration = animTime
        valueAnimator.addUpdateListener(ValueAnimator.AnimatorUpdateListener { animation ->
            Log.i(
                "Circle",
                "onAnimationUpdate: animation.getAnimatedValue()::" + animation.animatedValue
            )
            val i = Integer.valueOf(animation.animatedValue.toString())
            //每个单位长度占多少度
            progress = (360 * (i / 100f))
            Log.i("Circle", "onAnimationUpdate: mSelectRing::$progress")
            invalidate()
        })
        valueAnimator.start()
    }
}